package com.htandroid.htandroid.aty;

/**
 * Created by Administrator on 2015/12/26.
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbDialogFragment;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.entity.GcwxVouchs;
import com.htandroid.htandroid.entity.Inventory;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;
import com.htandroid.htandroid.sys.InstorageURL;

import java.math.BigDecimal;
import java.util.ArrayList;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

public class Gcwx_inventory extends AbActivity {
    public MyApplication myApplication;
    private GcwxList vouch;
    private GcwxVouchs vouchs;
    private ArrayList<GcwxVouchs> vouchslist;
    private String listtype = "0";
    private String ctype = "xd";
    private int icurrentrow;
    private ArrayList<Inventory> cinvlist;

    private AbTitleBar mAbTitleBar = null;
    private View rightView1 = null;
    private View rightView2 = null;
    private View rightView3 = null;
    private TextView tv_currentrow;
    private AbLoadDialogFragment mDialogFragment = null;

    @AbIocView(id = R.id.iquantity_et)
    EditText iquantity_et;
    @AbIocView(id = R.id.iprice_et)
    EditText iprice_et;
    @AbIocView(id = R.id.cinvcode_tv)
    TextView cinvcode_tv;
    @AbIocView(id = R.id.cinvname_tv)
    TextView cinvname_tv;
    @AbIocView(id = R.id.cinvstd_tv)
    TextView cinvstd_tv;
    @AbIocView(id = R.id.cinvunit_tv)
    TextView cinvunit_tv;
    @AbIocView(id = R.id.iamount_tv)
    TextView iamount_tv;
    @AbIocView(id = R.id.cobj_tv)
    TextView cobj_tv;
    @AbIocView(id = R.id.cs_iquantity_tv)
    TextView cs_iquantity_tv;
    @AbIocView(id = R.id.cs_icquantity_tv)
    TextView cs_icquantity_tv;
    @AbIocView(id = R.id.cs_idquantity_tv)
    TextView cs_idquantity_tv;
    @AbIocView(id = R.id.cinv_iv)
    ImageView cinv_iv;
    @AbIocView(id = R.id.cobj_iv)
    ImageView cobj_iv;
    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;
    @AbIocView(id = R.id.btn2_tv, click = "btnClick")
    TextView btn2_tv;
    @AbIocView(id = R.id.btn3_tv, click = "btnClick")
    TextView btn3_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_inventory);
        myApplication = (MyApplication) abApplication;
        initview();
        //已有材料显示,没有则默认增加一行并显示
        if (vouchslist != null && vouchslist.size() > 0) {
            insert_data(icurrentrow);
            mDialogFragment = AbDialogUtil.showLoadDialog(this, R.layout.view_dialog_loading, "正在加载...");
            mDialogFragment.setAbDialogOnLoadListener(new AbDialogFragment.AbDialogOnLoadListener() {

                @Override
                public void onLoad() {
                    getcinvstore();
                }
            });
        } else {
            //增加空行
            GcwxVouchs ivouch = new GcwxVouchs();
            //初始数据把所有列置空
            ivouch.init(vouch.getCvouchid());
            vouchslist.add(ivouch);
            insert_data(vouchslist.size() - 1);
        }
    }

    public void btnClick(View v) {
        switch (v.getId()) {
            case R.id.btn1_tv:
                btn1_click(v);
                break;
            case R.id.btn2_tv:
                btn2_click(v);
                break;
            case R.id.btn3_tv:
                btn3_click(v);
                break;
        }
    }

    //初始化
    private void initview() {
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("维修材料");
        mAbTitleBar.setLogo(R.drawable.icon_back);
        mAbTitleBar.getLogoView().setLayoutParams(new LinearLayout.LayoutParams(58, 57));
        mAbTitleBar.getLogoView().setFocusable(true);
        mAbTitleBar.getLogoView().setFocusableInTouchMode(true);
        mAbTitleBar.setTitleBarBackground(R.color.common_title);
        mAbTitleBar.setTitlePadding(118, 0, 0, 0);

        mAbTitleBar.clearRightView();
        rightView1 = mInflater.inflate(R.layout.btn_abs_back, null);
        mAbTitleBar.addRightView(rightView1);
        rightView2 = mInflater.inflate(R.layout.tv_currentrow, null);
        mAbTitleBar.addRightView(rightView2);
        rightView3 = mInflater.inflate(R.layout.btn_abs_forward, null);
        mAbTitleBar.addRightView(rightView3);
        tv_currentrow = (TextView) rightView2.findViewById(R.id.tv_currentrow);
        tv_currentrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                more_list(v);
            }
        });

        //获取传递的对象参数
        Intent intent = getIntent();
        vouchslist = (ArrayList<GcwxVouchs>) intent.getSerializableExtra("vouchslist");
        vouch = (GcwxList) intent.getSerializableExtra("vouch");
        listtype = intent.getExtras().getString("listtype");
        ctype = intent.getExtras().getString("ctype");
        icurrentrow = getIntent().getExtras().getInt("currentrow");

        //通过数量修改的监听
        iquantity_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
                return false;
            }
        });

        //通过数量焦点变化
        iquantity_et.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
            }
        });

        //单价修改的监听
        iprice_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
                return false;
            }
        });

        //单价焦点变化
        iprice_et.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    String iquantity = iquantity_et.getText().toString();
                    String iprice = iprice_et.getText().toString();
                    if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
                    } else {
                        iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
                    }
                }
            }
        });
    }

    public void getcinvstore() {
        String cinvcode = "";
        cinvcode = vouchslist.get(icurrentrow).getCinvcode();
//        TreeMap<String, String> properties = new TreeMap<String, String>();
//        properties.put("arg0", myApplication.getGs_ztid());
//        properties.put("arg1", myApplication.getGs_year());
//        properties.put("arg2", vouch.getCvouchid());
//        properties.put("arg3", cinvcode);
//        ToolSOAP.callService(InstorageURL.SERVERURL, InstorageURL.NAME_SPACE, "getinvgcstore", properties, new ToolSOAP.WebServiceCallBack() {
//            @Override
//            public void onSucced(SoapObject result) {
//                if (result != null) {
//                    //解析xml文件,返回列表(现存量信息)
//                    SoapObject res = (SoapObject) result.getProperty(0);
//                    String cs_icquantity = res.getProperty(0).toString();
//                    String cs_idquantit = res.getProperty(1).toString();
//                    String cs_iquantity = "" + (Double.valueOf(cs_icquantity) - Double.valueOf(cs_idquantit));
//                    //修改现存量信息
//                    vouchslist.get(icurrentrow).setCs_icquantity(cs_icquantity);
//                    vouchslist.get(icurrentrow).setCs_idquantity(cs_idquantit);
//                    vouchslist.get(icurrentrow).setCs_iquantity(cs_iquantity);
//                    cs_iquantity_tv.setText(Basenvo.customFormat("######.00##", cs_iquantity));
//                    cs_icquantity_tv.setText(Basenvo.customFormat("######.00##", cs_icquantity));
//                    cs_idquantity_tv.setText(Basenvo.customFormat("######.00##", cs_idquantit));
//                } else {
//                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                }
//                AbDialogUtil.removeDialog(Gcwx_inventory.this);
//            }
//
//            @Override
//            public void onFailure(String result) {
//                AbDialogUtil.removeDialog(Gcwx_inventory.this);
//                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//            }
//        });

        String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
        AbSoapParams params = new AbSoapParams();
        params.put("arg0", myApplication.getGs_ztid());
        params.put("arg1", myApplication.getGs_year());
        params.put("arg2", vouch.getCvouchid());
        params.put("arg3", cinvcode);
        mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "getinvgcstore", params, new AbSoapListener() {
            //获取数据成功会调用这里
            @Override
            public void onSuccess(int statusCode, String content) {
                if (content != null) {
                    String cs_icquantity = content.split("string=")[1].split(";")[0];
                    String cs_idquantit = content.split("string=")[2].split(";")[0];
                    String cs_iquantity = "" + (Double.valueOf(cs_icquantity) - Double.valueOf(cs_idquantit));
                    vouchslist.get(icurrentrow).setCs_icquantity(cs_icquantity);
                    vouchslist.get(icurrentrow).setCs_idquantity(cs_idquantit);
                    vouchslist.get(icurrentrow).setCs_iquantity(cs_iquantity);
                    cs_iquantity_tv.setText(Basenvo.customFormat("######.00##", cs_iquantity));
                    cs_icquantity_tv.setText(Basenvo.customFormat("######.00##", cs_icquantity));
                    cs_idquantity_tv.setText(Basenvo.customFormat("######.00##", cs_idquantit));
                } else {
                    AbToastUtil.showToast(Gcwx_inventory.this, R.string.sqlerrormsg);
                }
                AbDialogUtil.removeDialog(Gcwx_inventory.this);
            }

            // 失败，调用
            @Override
            public void onFailure(int statusCode, String content,
                                  Throwable error) {
                AbToastUtil.showToast(Gcwx_inventory.this, R.string.sqlerrormsg);
            }

            // 开始执行前
            @Override
            public void onStart() {
                //显示进度框
                //AbDialogUtil.showProgressDialog(Gcwx_inventory.this, 0, "正在加载...");
            }

            // 完成后调用，失败，成功
            @Override
            public void onFinish() {
                //移除进度框
                AbDialogUtil.removeDialog(Gcwx_inventory.this);
            }
        });
    }

    public void getcinvprice() {
        if (cinvcode_tv.getText() != null) {
            String cinvcode = cinvcode_tv.getText().toString();
//            TreeMap<String, String> properties = new TreeMap<String, String>();
//            properties.put("arg0", myApplication.getGs_ztid());
//            properties.put("arg1", myApplication.getGs_year());
//            properties.put("arg2", vouch.getDdate());
//            properties.put("arg3", cinvcode);
//            //ToolSOAPxml.callService("getinvstore", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//            ToolSOAP.callService(InstorageURL.SERVERURL, InstorageURL.NAME_SPACE, "getinvgcprice", properties, new ToolSOAP.WebServiceCallBack() {
//                @Override
//                public void onSucced(SoapObject result) {
//                    if (result != null) {
//                        //解析xml文件,返回列表(现存量信息)
//                        String res = result.getProperty(0).toString();
//                        //修改单价
//                        vouchslist.get(icurrentrow).setIprice(res);
//                        iprice_et.setText(Basenvo.customFormat("######.00##", res));
//                        String iquantity = iquantity_et.getText().toString();
//                        //根据数量计算金额
//                        if (iquantity == null || iquantity.equals("") || res.equals("")) {
//                            iamount_tv.setText(Basenvo.customFormat("######.00##", "0"));
//                        } else {
//                            iamount_tv.setText(Basenvo.customFormat("######.00##", Double.valueOf(iquantity) * Double.valueOf(res)));
//                        }
//                    } else {
//                        AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                    }
//                    AbDialogUtil.removeDialog(Gcwx_inventory.this);
//                }
//
//                @Override
//                public void onFailure(String result) {
//                    AbDialogUtil.removeDialog(Gcwx_inventory.this);
//                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                }
//            });

            String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
            AbSoapParams params = new AbSoapParams();
            params.put("arg0", myApplication.getGs_ztid());
            params.put("arg1", myApplication.getGs_year());
            params.put("arg2", vouch.getDdate());
            params.put("arg3", cinvcode);
            mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "getinvgcprice", params, new AbSoapListener() {
                //获取数据成功会调用这里
                @Override
                public void onSuccess(int statusCode, String content) {
                    if (content != null) {
                        String res = content.split("=")[1].split(";")[0];
                        //修改单价
                        vouchslist.get(icurrentrow).setIprice(res);
                        iprice_et.setText(Basenvo.customFormat("######.00##", res));
                        String iquantity = iquantity_et.getText().toString();
                        //根据数量计算金额
                        if (iquantity == null || iquantity.equals("") || res.equals("")) {
                            iamount_tv.setText(Basenvo.customFormat("######.00##", "0"));
                        } else {
                            iamount_tv.setText(Basenvo.customFormat("######.00##", Double.valueOf(iquantity) * Double.valueOf(res)));
                        }
                    } else {
                        AbToastUtil.showToast(Gcwx_inventory.this, R.string.sqlerrormsg);
                    }
                    AbDialogUtil.removeDialog(Gcwx_inventory.this);
                }

                // 失败，调用
                @Override
                public void onFailure(int statusCode, String content,
                                      Throwable error) {
                    AbToastUtil.showToast(Gcwx_inventory.this, R.string.sqlerrormsg);
                }

                // 开始执行前
                @Override
                public void onStart() {
                    //显示进度框
                    //AbDialogUtil.showProgressDialog(Gcwx_inventory.this, 0, "正在加载...");
                }

                // 完成后调用，失败，成功
                @Override
                public void onFinish() {
                    //移除进度框
                    AbDialogUtil.removeDialog(Gcwx_inventory.this);
                }
            });
        }
    }

    //为窗口填充数据
    public void insert_data(int row) {
        vouchs = vouchslist.get(row);
        icurrentrow = row;
        tv_currentrow.setText((icurrentrow + 1) + "/" + (vouchslist.size()));
        //增加/删除按钮
        if (listtype.equals("0")) {
            showwin(true);
        } else {
            showwin(false);
        }
        //写入数据
        iquantity_et.setText(Basenvo.customFormat("######.00##", vouchs.getIquantity()));
        iprice_et.setText(Basenvo.customFormat("######.00##", vouchs.getIprice()));
        cinvcode_tv.setTag(vouchs.getCinvcode());
        cinvname_tv.setText(vouchs.getCinvname());
        cinvstd_tv.setText(vouchs.getCinvstd());
        cinvunit_tv.setText(vouchs.getCinvm_unit());
        iamount_tv.setText(Basenvo.customFormat("######.00##", vouchs.getIamount()));
        cobj_tv.setText(vouchs.getCobjname());
        cobj_tv.setTag(vouchs.getCobjcode());

        cs_iquantity_tv.setText(Basenvo.customFormat("######.00##", vouchs.getCs_iquantity()));
        cs_icquantity_tv.setText(Basenvo.customFormat("######.00##", vouchs.getCs_icquantity()));
        cs_idquantity_tv.setText(Basenvo.customFormat("######.00##", vouchs.getCs_idquantity()));
    }


    public void showwin(boolean stype) {
        //背景变白，字变黑色
        Integer backgroundcolor = Color.TRANSPARENT;
        if (ctype.equals("check") && stype) {
            //审核时,只允许修改成本对象
            iquantity_et.setEnabled(false);
            iprice_et.setEnabled(false);
            cinv_iv.setVisibility(View.GONE);
            cinvcode_tv.setBackgroundColor(backgroundcolor);
            iquantity_et.setBackgroundColor(backgroundcolor);
            iprice_et.setBackgroundColor(backgroundcolor);
            //审核时,只允许修改成本对象
            cobj_iv.setVisibility(View.VISIBLE);
            cobj_tv.setBackgroundResource(R.drawable.edit_background);
            //隐藏新增和删除
            btn1_tv.setVisibility(View.GONE);
            btn2_tv.setVisibility(View.GONE);
        } else {
            iquantity_et.setEnabled(stype);
            iprice_et.setEnabled(stype);
            if (stype) {
                cinv_iv.setVisibility(View.VISIBLE);
                cobj_iv.setVisibility(View.VISIBLE);

                cinvcode_tv.setBackgroundResource(R.drawable.edit_background);
                cobj_tv.setBackgroundResource(R.drawable.edit_background);
                iquantity_et.setBackgroundResource(R.drawable.edit_background);
                iprice_et.setBackgroundResource(R.drawable.edit_background);

                //隐藏修改,删除确定
                btn1_tv.setVisibility(View.VISIBLE);
                btn2_tv.setVisibility(View.VISIBLE);
                btn3_tv.setVisibility(View.VISIBLE);
            } else {
                cinv_iv.setVisibility(View.GONE);
                cobj_iv.setVisibility(View.GONE);

                cinvcode_tv.setBackgroundColor(backgroundcolor);
                cobj_tv.setBackgroundColor(backgroundcolor);
                iquantity_et.setBackgroundColor(backgroundcolor);
                iprice_et.setBackgroundColor(backgroundcolor);

                //隐藏修改,删除确定
                btn1_tv.setVisibility(View.GONE);
                btn2_tv.setVisibility(View.GONE);
                btn3_tv.setVisibility(View.GONE);
            }
        }
    }

    //存货切换
    public void more_list(View v) {
        updatedataref();
        if (!prechecked()) return;
        Intent intent = new Intent(getApplicationContext(), Gcwx_inventory_detaillist.class);
        //传递参数
        Bundle bundle = new Bundle();
        intent.putExtra("currentrow", "" + icurrentrow);//当前存货行号
        bundle.putSerializable("vouchslist", vouchslist);//存货明细列表
        intent.putExtras(bundle);
        startActivityForResult(intent, R.layout.layout_gcwx_inventory_detaillist / 500000);
    }

    //新增空行
    public void btn1_click(View v) {
        updatedataref();
        if (!prechecked()) return;
        GcwxVouchs ivouch = new GcwxVouchs();
        //初始数据把所有列置空
        ivouch.init(vouch.getCvouchid());
        vouchslist.add(ivouch);
        insert_data(vouchslist.size() - 1);
    }

    //删除当前行
    public void btn2_click(View v) {
        vouchslist.remove(icurrentrow);
        //最后一行删除则需要顺序上移
        if (icurrentrow > vouchslist.size() - 1) {
            //如果已经删完数据,则添加一个空行数据
            if (vouchslist.size() == 0) {
                GcwxVouchs ivouch = new GcwxVouchs();
                //初始数据把所有列置空
                ivouch.init(vouch.getCvouchid());
                vouchslist.add(ivouch);
            }
            insert_data(vouchslist.size() - 1);
        } else {
            insert_data(icurrentrow);
        }
    }

    //确定返回上一页面
    public void btn3_click(View v) {
        updatedataref();
        if (!prechecked()) return;
        Intent intent = new Intent(getApplicationContext(), Gcwx_yh_detail.class);
        //传递参数
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchslist", vouchslist);
        intent.putExtras(bundle);
        setResult(R.layout.layout_gcwx_inventory / 200000, intent);  //设置返回结果
        //打开延缓窗口,关闭当前窗口
        this.finish();
    }

    //对一些修改后的值进行处理,避免因为没触发事件导致保存的信息不正确
    public void updatedataref() {
        String iquantity = iquantity_et.getText().toString();
        String iprice = iprice_et.getText().toString();
        if (iquantity == null || iquantity.equals("") || iprice.equals("")) {
            iamount_tv.setText(Basenvo.customFormat("#####0.00##", "0"));
        } else {
            iamount_tv.setText(Basenvo.customFormat("#####0.00##", Double.valueOf(iquantity) * Double.valueOf(iprice)));
        }
        //保存修改内容
        if (iquantity_et.getText() != null) {
            vouchs.setIquantity(iquantity_et.getText().toString());
        }
        if (iprice_et.getText() != null) {
            vouchs.setIprice(iprice_et.getText().toString());
        }
        if (iamount_tv.getText() != null) {
            vouchs.setIamount(iamount_tv.getText().toString());
        }
    }

    //检测数据是否合法
    public boolean prechecked() {
        if (listtype.equals("1") || ctype.equals("check")) return true;
        //判断现存量
        String Cs_iquantity = vouchs.getCs_iquantity();
        if (Cs_iquantity == null || Cs_iquantity.equals("")) {
            Cs_iquantity = "0";
        }
        String iquantity = vouchs.getIquantity();
        if (iquantity == null || iquantity.equals("")) {
            iquantity = "0";
        }
        //数量大于可销量
        if (Basenvo.compare(BigDecimal.valueOf(Double.valueOf(iquantity)), BigDecimal.valueOf(Double.valueOf(Cs_iquantity))).equals(">")) {
            AbToastUtil.showToastInThread(getApplicationContext(), "使用数量不能大于可销量!");
            return false;
        }
        return true;
    }

    //选择存货
    public void cinv_choose(View v) {
        String btntype = v.getTag().toString();
        String cinvcode = "", filter = "";
        cinvcode = cinvcode_tv.getTag().toString();

        //设置过滤条件
        filter = "gcwxinv";
        Intent intent = new Intent(getApplicationContext(), Basenvo_Chooseinventory.class);
        // 设置传递数据
        intent.putExtra("btntype", btntype);
        intent.putExtra("ccode", cinvcode);
        intent.putExtra("filter", filter);
        startActivityForResult(intent, R.layout.basenvo_rchoose / 200000);
    }

    //选择成本对象
    public void cobj_choose(View v) {
        String btntype = v.getTag().toString();
        String cobjcode = "";
        cobjcode = cobj_tv.getTag().toString();
        //设置过滤条件
        Intent intent = new Intent(getApplicationContext(), Basenvo_Choosecostobj.class);
        // 设置传递数据
        intent.putExtra("btntype", btntype);
        intent.putExtra("ccode", cobjcode);
        startActivityForResult(intent, R.layout.basenvo_rchoose / 200000);
    }

    //向左翻页
    public void btn_abs_back(View v) {
        updatedataref();
        if (!prechecked()) return;
        if (icurrentrow == 0) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是第一项!");
        } else {
            insert_data(icurrentrow - 1);
            //翻页后,获取当前库存量
            getcinvstore();
        }
    }

    //向右翻页
    public void btn_abs_forward(View v) {
        updatedataref();
        if (!prechecked()) return;
        if (icurrentrow == (vouchslist.size() - 1)) {
            AbToastUtil.showToastInThread(getApplicationContext(), "已经是最后一项!");
        } else {
            insert_data(icurrentrow + 1);
            //翻页后,获取当前库存量
            getcinvstore();
        }
    }

    //接收返回数据(存货明细,行切换,成本对象)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.basenvo_rchoose / 200000:
                if (data != null) {
                    cinvlist = myApplication.getGv_cinv();
                    //刷新list
                    Bundle bundle = data.getExtras();
                    String ccode = bundle.getString("ccode");
                    String cname = bundle.getString("cname");
                    String btntype = bundle.getString("btntype");
                    if (btntype.equals("cinv")) {
                        int irow = bundle.getInt("irow");
                        //存货则必须从列表中获取.
                        cinvcode_tv.setText(ccode);
                        cinvname_tv.setText(cname);
                        vouchs.setCinvcode(ccode);
                        vouchs.setCinvname(cname);

                        if (irow > 0) {
                            cinvunit_tv.setText(cinvlist.get(irow).getCinvm_unit());
                            cinvstd_tv.setText(cinvlist.get(irow).getCinvstd());
                            vouchs.setCinvm_unit(cinvlist.get(irow).getCinvm_unit());
                            vouchs.setCinvstd(cinvlist.get(irow).getCinvstd());
                            //获取单价
                            mDialogFragment = AbDialogUtil.showLoadDialog(this, R.layout.view_dialog_loading, "正在加载...");
                            mDialogFragment.setAbDialogOnLoadListener(new AbDialogFragment.AbDialogOnLoadListener() {

                                @Override
                                public void onLoad() {
                                    //获取单价
                                    getcinvprice();
                                    //获取现存量
                                    getcinvstore();
                                }
                            });
                        } else {
                            cinvunit_tv.setText("");
                            cinvstd_tv.setText("");
                            iprice_et.setText("");
                            iamount_tv.setText("");
                            cs_iquantity_tv.setText(Basenvo.customFormat("######.00##", ""));
                            cs_icquantity_tv.setText(Basenvo.customFormat("######.00##", ""));
                            cs_idquantity_tv.setText(Basenvo.customFormat("######.00##", ""));

                            vouchs.setCs_icquantity("");
                            vouchs.setCs_idquantity("");
                            vouchs.setCs_iquantity("");
                            vouchs.setCinvm_unit("");
                            vouchs.setCinvstd("");
                        }

                    } else if (btntype.equals("cobj")) {
                        //成本对象选择
                        cobj_tv.setText(cname);
                        cobj_tv.setTag(ccode);
                        vouchs.setCobjcode(ccode);
                        vouchs.setCobjname(cname);
                    }
                }
                break;
            case R.layout.layout_gcwx_inventory_detaillist / 500000:
                Bundle bundle = data.getExtras();
                String listrow = bundle.getString("listrow");
                insert_data(Integer.valueOf(listrow));
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

